package com.glory.good_dark.activity;

import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.MaterielFoolProofingListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.MaterielFoolProofingEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: MaterielFoolProofingActivity
 * Author: XJH
 * Date: 2020/5/18 11:44
 * Description:
 * History:
 */
public class MaterielFoolProofingActivity extends BaseProjectActivity {
    @BindView(R.id.EQPID_Txt)
    TextView EQPIDTxt;
    @BindView(R.id.tvEqpStatus)
    TextView tvEqpStatus;
    @BindView(R.id.adjustable_But)
    Button btnAdjustable;
    @BindView(R.id.replaceMateriel_But)
    Button replaceMaterielBut;
    @BindView(R.id.mtl_Lay)
    LinearLayout mtlLay;
    @BindView(R.id.materiel_Rv)
    RecyclerView materielRv;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.flPositionList)
    FrameLayout flPositionList;
    @BindView(R.id.tvPosition)
    TextView tvPosition;
    @BindView(R.id.ivPositionArrow)
    ImageView ivPositionArrow;
    private MaterielFoolProofingListAdapter foolProofingListAdapter;
    private FreeUI_GeneralPop positionPop;
    private int MATERIEL_ON = 33;
    private List<String> typeList = new ArrayList<>();

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {
        if (tag == MATERIEL_ON){
            //设备上料
//            showToast("上料成功" + content,true);
            FreeUI_EntityCallBack<BaseEntity<JsonObject>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<JsonObject>>
                    (new TypeToken<BaseEntity<JsonObject>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess
                        (final Response<BaseEntity<JsonObject>> response) {
                    if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                        //showToast("上料成功",true);
                        initData();
                        showConfirmDialog(MaterielFoolProofingActivity.this,response.body().getData().getReturn()
                                .getRETURNMESSAGE().toString());//"上料成功"
                    }
                }

                @Override
                public void onError
                        (Response<BaseEntity<JsonObject>> response) {
                    super.onError(response);
                    loadError(response.getException(), "PDAONEQPMat");
                }
            };

            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("EQP_ID", App.EQPID);
            jsonObject.addProperty("MATERIAL_ID", content);
            jsonObject.addProperty("MTMTYPE", tvPosition.getText().toString());
            NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAONEQPMat", jsonObject);

        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_materiel_foolproofing;
    }

    @Override
    protected void initView() {
        EQPIDTxt.setText(App.EQPID);
        if (App.EQPID.startsWith("WB")){
            typeList.add("CP");
            typeList.add("GW");
        }else if (App.EQPID.startsWith("DA")){
            typeList.add("EP");
            typeList.add("LF");
            typeList.add("PI");
            typeList.add("RT");
            typeList.add("WF");
        }else if (App.EQPID.startsWith("WS") || App.EQPID.startsWith("SS")){
            typeList.add("BS");
        }
        initRecyclerView(materielRv,this);
        foolProofingListAdapter = new MaterielFoolProofingListAdapter(null);
        foolProofingListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        foolProofingListAdapter.isFirstOnly(true);
        materielRv.setAdapter(foolProofingListAdapter);

        foolProofingListAdapter.setOnOffEQPMatClickListener((v, item) -> {
            //确定下料
//            showToast("下料成功" + item.getMAT_NO(),true);
            FreeUI_EntityCallBack<BaseEntity<JsonObject>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<JsonObject>>
                    (new TypeToken<BaseEntity<JsonObject>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess
                        (final Response<BaseEntity<JsonObject>> response) {
                    if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                        showToast("下料成功",true);
                        initData();
//                        showConfirmDialog(MaterielFoolProofingActivity.this,response.body().getData().getReturn()
//                        .getRETURNMESSAGE().toString());//"下料成功"
                    }
                }

                @Override
                public void onError
                        (Response<BaseEntity<JsonObject>> response) {
                    super.onError(response);
                    loadError(response.getException(), "PDAOFFEQPMat");
                }
            };

            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("EQP_ID", App.EQPID);
            jsonObject.addProperty("MATERIAL_TYPE", item.getMATERIAL_TYPE());
            jsonObject.addProperty("MAT_NO", item.getMAT_NO());
            jsonObject.addProperty("MATERIAL_ID", item.getMATERIAL_ID());
            NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAOFFEQPMat", jsonObject);
        });

    }

    @Override
    protected void initData() {
        getEqpState(tvEqpStatus,this);
//        String ss = "{\n" +
//                "    \"data\":{\n" +
//                "        \"BODY\":{\n" +
//                "            \"MTM_MAT_LIST\":[\n" +
//                "                {\n" +
//                "                    \"RUN_ID\":\"2\",\n" +
//                "                    \"EQP_ID\":\"WB135\",\n" +
//                "                    \"DESCRIPTION\":\"0\",\n" +
//                "                    \"MATERIAL_ID\":null,\n" +
//                "                    \"MAT_NO\":null,\n" +
//                "                    \"SUB_TYPE\":null,\n" +
//                "                    \"SPECS\":null,\n" +
//                "                    \"MATERIAL_TYPE\":\"CP\",\n" +
//                "                    \"MATERIAL_NAME\":null,\n" +
//                "                    \"ONEQP_TIME\":null,\n" +
//                "                    \"OFFEQP_TIME\":null,\n" +
//                "                    \"EXPIRATION_TIME\":null\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"RUN_ID\":null,\n" +
//                "                    \"EQP_ID\":\"WB135\",\n" +
//                "                    \"DESCRIPTION\":\"1\",\n" +
//                "                    \"MATERIAL_ID\":\"\",\n" +
//                "                    \"MAT_NO\":\"\",\n" +
//                "                    \"SUB_TYPE\":null,\n" +
//                "                    \"SPECS\":null,\n" +
//                "                    \"MATERIAL_TYPE\":\"GW\",\n" +
//                "                    \"MATERIAL_NAME\":\"\",\n" +
//                "                    \"ONEQP_TIME\":\"2020/5/25 9:31:05\",\n" +
//                "                    \"OFFEQP_TIME\":\"0001/1/1 0:00:00\",\n" +
//                "                    \"EXPIRATION_TIME\":\"0001/1/1 0:00:00\"\n" +
//                "                }\n" +
//                "            ],\n" +
//                "            \"EQP_ID\":\"WB135\"\n" +
//                "        },\n" +
//                "        \"HEADER\":{\n" +
//                "            \"MACHINENAME\":\"PDA-MT9001\",\n" +
//                "            \"MESSAGENAME\":\"PDAEQPGETMATInfo\",\n" +
//                "            \"TRANSACTIONID\":\"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "            \"EVENTTIME\":\"2019-03-16 16:28:59.585\"\n" +
//                "        },\n" +
//                "        \"RETURN\":{\n" +
//                "            \"RETURNCODE\":1,\n" +
//                "            \"RETURNMESSAGE\":\"获取设备WB135当前物料成功\"\n" +
//                "        }\n" +
//                "    }\n" +
//                "}";
//        BaseEntity<MaterielFoolProofingEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<MaterielFoolProofingEntity>>() {}.getType());
//        if (bean.isSuccess(MaterielFoolProofingActivity.this,getSupportFragmentManager(),true)){
//            foolProofingListAdapter.setNewData(bean.getData().getBody().getMTM_MAT_LIST());
//        }


        FreeUI_EntityCallBack<BaseEntity<MaterielFoolProofingEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<MaterielFoolProofingEntity>>
                (new TypeToken<BaseEntity<MaterielFoolProofingEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<MaterielFoolProofingEntity>> response) {
                if (response.body().isSuccess(MaterielFoolProofingActivity.this,getSupportFragmentManager(),true)) {
                    foolProofingListAdapter.setNewData(response.body().getData().getBody().getMTM_MAT_LIST());
                }


            }

            @Override
            public void onError
                    (Response<BaseEntity<MaterielFoolProofingEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAEQPGETMATInfo");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAEQPGETMATInfo", jsonObject);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("物料防呆");
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @OnClick({R.id.adjustable_But, R.id.replaceMateriel_But,R.id.flPositionList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.adjustable_But:
                materielRv.setVisibility(materielRv.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                btnAdjustable.setText(materielRv.getVisibility() == View.VISIBLE ? "收起" : "展开");
                break;
            case R.id.replaceMateriel_But:
                if (tvPosition.getText().toString().isEmpty()){
                    showToast("请先选择物料类型",false);
                    return;
                }
                showSpanDialog(MATERIEL_ON,"请扫描物料条码");
                break;
            case R.id.flPositionList:
                if (positionPop != null) {
                    if (positionPop.isShowing()) {
                        positionPop.dismiss();
                    }
                } else {

                    positionPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPositionList.getWidth(), typeList, ivPositionArrow, (code, i) -> {
                        tvPosition.setText(code);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivPositionArrow);
                positionPop.showAsDropDown(flPositionList, 0, 0);
                break;
        }
    }
}
